import {
  getChecks
} from '../../../utils/networkRequest.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [{
        id: '0',
        title: '全部检测'
      },
      {
        id: '3',
        title: '检测完成'
      },
      {
        id: '2',
        title: '未转化'
      }
    ],
    selectedId: '0',
    checksInfo: null,
    page: 1,
    last_page: -1, //最后一页
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let self = this;
    getChecks(this.data.selectedId,this.data.page, (r) => {
      self.setData({
        checksInfo: r.data,
        last_page: r.pager.last_page
      })
    })
  },
  handleTabChange: function(res) {

    let self = this;
    getChecks(res.detail,this.data.page, (r) => {
      self.setData({
        checksInfo: r.data,
        selectedId: res.detail,
        last_page: r.pager.last_page
      })
    })
  },
  //下啦刷新
  onPullDownRefresh: function () {
    let self = this;
    let { 
      last_page,
      selectedId,
    } = this.data;
    getChecks(selectedId,1, (r) => {
      self.setData({
        checksInfo: r.data,
        page: 1,
        last_page: r.pager.last_page
      })
      wx.stopPullDownRefresh()
    })
  },
  //上拉加载
  onReachBottom: function() {
    var self = this;
    let {
      page,
      last_page,
      selectedId,
    } = this.data;

    if (last_page <= page) {
      wx.showToast({
        icon: "none",
        title: '没有更多数据了.'
      })
    } else {
      page = page + 1;
      getChecks(selectedId,page, (r) => {
        console.log(r);
        self.data.checksInfo.list =  self.data.checksInfo.list.concat(r.data.list); 
        self.setData({
          checksInfo: self.data.checksInfo,
          page: page,
          last_page: r.pager.last_page
        })
      })
    }

  },
  //跳转至检测详情
  goCheckOrderDetail: function(e) {

    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: "../../jiechedanbox/checkDetail/checkDetail?id=" + id
    })
  }
})